<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>台球吧管理系统</h2>
        <p>欢迎登录</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-position="top"
        class="login-form"
      >
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="loginForm.phone"
            placeholder="请输入手机号"
            prefix-icon="Phone"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <div class="login-options">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
          <el-link type="primary" :underline="false">忘记密码？</el-link>
        </div>
        
        <el-button
          type="primary"
          :loading="loading"
          class="login-button"
          @click="handleLogin"
        >
          登录
        </el-button>
        
        <div class="register-link">
          还没有账号？
          <el-link type="primary" @click="$router.push('/register')">立即注册</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useUserStore } from '../store/modules/user';
import { ElMessage } from 'element-plus';

export default {
  name: 'LoginView',
  
  setup() {
    const router = useRouter();
    const route = useRoute();
    const userStore = useUserStore();
    const loginFormRef = ref(null);
    const loading = ref(false);
    const rememberMe = ref(false);
    
    // 登录表单
    const loginForm = reactive({
      phone: '13912345678',
      password: 'password'
    });
    
    // 表单验证规则
    const loginRules = {
      phone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
      ]
    };
    
    // 登录方法
    const handleLogin = async () => {
      await loginFormRef.value.validate(async (valid) => {
        if (!valid) return;
        
        loading.value = true;
        
        try {
          const success = await userStore.login(loginForm);
          
          if (success) {
            // 如果有重定向地址，则跳转到重定向地址
            const redirectPath = route.query.redirect || '/';
            router.push(redirectPath);
          }
        } catch (error) {
          ElMessage.error('登录失败，请检查用户名和密码');
          console.error('登录失败:', error);
        } finally {
          loading.value = false;
        }
      });
    };
    
    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      rememberMe,
      handleLogin
    };
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 24px;
  color: #409eff;
  margin-bottom: 10px;
}

.login-header p {
  font-size: 16px;
  color: #606266;
}

.login-form {
  margin-top: 20px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  margin-bottom: 20px;
}

.register-link {
  text-align: center;
  font-size: 14px;
  color: #606266;
}
</style> 